package com.zpark.it.controller;

import com.zpark.it.param.UserCheckParam;
import com.zpark.it.param.UserLoginParam;
import com.zpark.it.pojo.User;
import com.zpark.it.service.UserService;
import com.zpark.it.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Title: UserController 用户模块的控制类
 * @Author Charles
 * @Package com.zpark.it.controller
 * @Date 2023/7/19 21:40
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userservice;

    @PostMapping("/check")
    public R check(@RequestBody @Validated UserCheckParam userCheckParam, BindingResult result){

        //检查是否符合检验注解的规则，符合false，不符合true
        boolean b = result.hasErrors();
        if (b){
            return R.fail("账户为空，不可使用");
        }
        return userservice.check(userCheckParam);
    }

    /**
     * 注册
     * @param user
     * @param result
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody @Validated User user,BindingResult result){
        if (result.hasErrors()){
            //如果存在异常，证明请求参数不符合要求
            return R.fail("参数不符合要求,不可以注册");
        }
        return userservice.register(user);
    }
    @PostMapping("/login")
    public R login(@RequestBody @Validated UserLoginParam userLoginParam,BindingResult result){
        if (result.hasErrors()){
            //如果存在异常，证明请求参数不符合要求
            return R.fail("参数不符合要求,不可以登录");
        }

        return userservice.login(userLoginParam);
    }


}
